package com.jenkins.test.proxy.doscan;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @version V0.0.1
 * @ClassName: {@link ReferenceScanBeanDefinitionRegistrar}
 * @Description: ReferenceScanBeanDefinitionRegistrar
 * @author: Lanzhou
 * @date: 2022/1/23 16:00
 * @Copyright:2019 All rights reserved.
 * 注意：本内容仅限于本人开发与维护，禁止外泄以及用于其他的商业目
 */
public class ReferenceScanBeanDefinitionRegistrar implements ApplicationContextAware, ImportBeanDefinitionRegistrar {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        // 获取MapperScan注解属性信息
        AnnotationAttributes annotationAttributes = AnnotationAttributes
                .fromMap(importingClassMetadata.getAnnotationAttributes(ReferenceScan.class.getName()));
        // 获取注解的属性值,拿到定义的扫描路径
        assert annotationAttributes != null;
        String[] basePackages = annotationAttributes.getStringArray("basePackage");
        // 使用自定义扫描器扫描
        AnnotationScanner annotationScanner = new AnnotationScanner(registry);
        annotationScanner.setResourceLoader(applicationContext);

        // 循环扫描我们的包文件下下的文件
        annotationScanner.scan(basePackages);
        // "com.pepsi.annotationproxy.service"是我 接口所在的包
        // annotationScanner.scan("com.jenkins.test.proxy.doscan.service");
    }

}
